package cn.edu.wit.servlets;

import cn.edu.wit.beans.Clazz;
import cn.edu.wit.beans.Student;
import cn.edu.wit.dao.StudentDAO;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class StudentAddServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		//1.��������
		String stuNum = request.getParameter("num");
		String stuName = request.getParameter("name");
		String stuSex = request.getParameter("sex");
		
		int stuAge = Integer.parseInt(request.getParameter("age"));
		
		String stuTel = request.getParameter("tel");
		String stuQQ = request.getParameter("qq");
		String stuAddr = request.getParameter("addr");
		String stuRemark = request.getParameter("remark");
		
		int cid = Integer.parseInt(request.getParameter("class_id"));
		
		//2.�����յ���ѧ����Ϣ���浽һ��Student������
		Clazz cla = new Clazz();
		cla.setCid(cid);
		Student s = new Student(stuNum, stuName, stuSex, stuAge, stuTel, stuQQ, stuAddr, stuRemark,cla);
		
		
		//3.����StudentDAO�е�insertStudent�������浽���ݿ�
		StudentDAO sdao = new StudentDAO();
		boolean b = sdao.insertStudent(s);
		
		//4.��ת����ʾҳ��
		String tips = b?"<label style='color:green'>ѧ����Ϣ��ӳɹ���</label>":"<label style='color:red'>ѧ����Ϣ���ʧ�ܣ�</label>";
		request.setAttribute("tips", tips);
		request.getRequestDispatcher("prompt.jsp").forward(request, response);
		
	
	}
	
	

}
